package com.zdsoft.crm.controller.auth;

import java.util.UUID;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zdsoft.crm.controller.BaseController;
import com.zdsoft.crm.entity.auth.SysFunction;
import com.zdsoft.crm.service.auth.ISysFunctionService;
import com.zdsoft.crm.vo.AjaxVo;

@Controller
@RequestMapping("/auth/function")
public class FunctionController extends BaseController {

	@Autowired
	ISysFunctionService iSysFunctionService;

	@GetMapping("/list")
	public ModelAndView list(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		return new ModelAndView("/auth/function/list.html", "list", iSysFunctionService.findPage(pageNum, pageSize));
	}

	@GetMapping("/save")
	public String saveUI() {
		return "/auth/function/save.html";
	}

	@PostMapping("/save")
	@ResponseBody//ajax提交注解
	public AjaxVo save(@Valid SysFunction function,BindingResult result) {
		if (result.hasErrors()) {
			for (ObjectError error : result.getAllErrors()) {
				return new AjaxVo().setErrorCode("10002").setErrorMsg(error.getDefaultMessage());
			}
		}
		SysFunction say=new SysFunction();
		say.setId(UUID.randomUUID().toString().replace("-", ""));
		say.setFunc_code(function.getFunc_code());
		say.setFunc_name(function.getFunc_name());
		say.setFunc_type(function.getFunc_type());
		say.setFunc_url(function.getFunc_url());
		say.setParent_id(function.getParent_id());
		iSysFunctionService.save(say);
		return new AjaxVo().setErrorCode("0");
	}

	@GetMapping("/{id}")
	public ModelAndView updateUI(@PathVariable("id") String id) {
		SysFunction sysFunction = new SysFunction();
		sysFunction.setId(id);
		// iSysFunctionService.findOne(sysFunction);

		ModelAndView mav = new ModelAndView("/auth/function/update.html");
		// 1. 查询当前菜单信息
		mav.addObject("functionInfo", iSysFunctionService.findOne(sysFunction));

		return mav;
	}

	@PostMapping("/{id}")
	@ResponseBody//ajax提交注解
	public AjaxVo update(@PathVariable("id") String id, @Valid SysFunction function,BindingResult result) {
		if (result.hasErrors()) {
			for (ObjectError error : result.getAllErrors()) {
				return new AjaxVo().setErrorCode("10002").setErrorMsg(error.getDefaultMessage());
			}
		}
		SysFunction say=new SysFunction();
		say.setId(id);
		say.setFunc_code(function.getFunc_code());
		say.setFunc_name(function.getFunc_name());
		say.setFunc_type(function.getFunc_type());
		say.setFunc_url(function.getFunc_url());
		say.setUpdate_time(function.getUpdate_time());
		say.setParent_id(function.getParent_id());
		iSysFunctionService.update(say);
		return new AjaxVo().setErrorCode("0");
	}

	@DeleteMapping("/{id}")
	@ResponseBody//ajax提交注解
	public void delete(@PathVariable("id") String id) {
		iSysFunctionService.deleteById(id);
	}
}
